package com.talkweb.securitycamera.data.hadoopFile;

import com.talkweb.securitycamera.common.constans.FileType;
import com.talkweb.securitycamera.data.bean.MediaEntity;
import ohos.app.Context;

import java.util.List;

/**
 * @version 1.0.1
 * @description: policy to delete media,if memory is out,delete one oldest video or three oldest picture
 * @program: security-camera
 * @Author xiaozhijun
 * @Date 2021/2/3 17:23
 */
public class SimpleMemoryPolicy implements IMemoryPolicy {

    private String cachePath;

    public final int pictureDeleteCount = 3;

    public final int videoDeleteCount = 1;

    @Override
    public boolean isMemInfoOut() {
        return false;
    }

    @Override
    public void cleanMemory(Context context, FileType fileType) {
        BaseMediaProcessor mediaProcessor;
        if (fileType == FileType.FILE_TYPE_IMAGE) {
            mediaProcessor = new SimpleImageProcessor(context);
        } else {
            mediaProcessor = new SimpleVideoProcessor(context);
        }
        List<MediaEntity> mediaList = mediaProcessor.getFileList(fileType);
        mediaList.sort((t1, t2) -> (int) (t1.getCreateTime() - t2.getCreateTime()));

        int deleteCount;
        if (fileType == FileType.FILE_TYPE_IMAGE){
            deleteCount = pictureDeleteCount;
        }else {
            deleteCount = videoDeleteCount;
        }
        int size = mediaList.size();
        deleteCount = mediaList.size() <= deleteCount ? size : deleteCount;

        for (int i = 0; i < deleteCount; i++) {
            mediaList.get(i).delete();
        }
    }
}
